package math;

import io.GPSPoint;

import java.util.ArrayList;
import java.util.List;

import ui.TimeSeriesName;
import ui.TimeSeriesName.TimeSeriesType;

public class DistanceCalculator implements StatisticProvider {

	private List<GPSPoint> points;
	
	private double[] distances;
	
	private long[] times;

	private final int stepSize;

	public DistanceCalculator(int stepSize) {
		this.stepSize = stepSize;
	}
	
	@Override
	public TimeSeriesName getName(String name) {
		return new TimeSeriesName(TimeSeriesType.DISTANCE, name, "Distance Cumulative [" + stepSize + "]");
	}

	@Override
	public double[] getValues() {
		return distances;
	}

	@Override
	public long[] getTimes() {
		return times;
	}


	@Override
	public void setTrack(List<GPSPoint> gpsTracks) {
		this.points = gpsTracks;
		double dist = 0;
		List<Double> distanceL = new ArrayList<Double>();
		List<Long> timesL = new ArrayList<Long>();
		GPSPoint lastPoint = points.get(0);
		for (int i = 1; i < gpsTracks.size(); i += stepSize) {
			GPSPoint currentPoint = points.get(i);
			dist += Distance.distMeter(lastPoint, currentPoint);
			distanceL.add(dist);
			lastPoint = currentPoint;
			timesL.add(currentPoint.getTime());
		}
		distances = Utils.toDoubles(distanceL);
		times = Utils.toLongs(timesL);
	}

}
